﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebPagesStarterKit;
using System.Data;
using System.IO;

public partial class UserControls_ForumTeaser : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Directory.Exists(Server.MapPath("~/App_Data/Forum")))
        {
            if (Directory.GetFiles(Server.MapPath("~/App_Data/Forum")).Length > 0)
            {
                if (SectionLoader.GetInstance().LoadSections("Forum").Count > 0)
                {
                    int count = 0;
                    litEntries.Text = "<ul id=\"forumTeaser\">";

                    foreach (Forum section in SectionLoader.GetInstance().LoadSections("Forum"))
                    {
                        DataRow[] rows = section.GetThreads().Table.Select("IsApproved='true'", "lastEntry DESC", DataViewRowState.CurrentRows);
                        foreach (DataRow row in rows)
                        {
                            string title = Convert.ToString(row["Subject"]);

                            if (title.Length > 32)
                                title = title.Substring(0, 32) + "...";

                            litEntries.Text += string.Format("<li><a href=\"/{0}?thread={1}#{2}\">{3}</a></li>",
                                                            new WebPage(section.PageID).VirtualPath.Replace("~/", ""),
                                                            Convert.ToString(row["Permalink"]),
                                                            section.SectionId
                                                            , title);
                            count++;
                            if (count > 2)
                                break;
                        }

                    }
                    litEntries.Text += "</ul>";
                }
            }
        }
    }
}
